package controlleur;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.ArrayList;

import json.JsonParserPromotion;
import json.JsonParserSuccursale;
import messages.SystemMessage;
import util.HOST;

import data.Promotion;
import data.Succursale;

public class promotionControlleur {
	
	public ArrayList<Promotion> getPromotionFromDetaillant(int idDet)
	{
		ArrayList<Promotion> promotions = new ArrayList<Promotion>();		

		String strUrl  =("http://"+HOST.HOST+"/proxirabaisServer/PromotionServlet?"+SystemMessage.ACTION+"="+SystemMessage.GET_ALL_DETAILLANT_PROMOTION+"&"+SystemMessage.DETAILLANT_ID+"="+idDet);
		//System.out.println(strUrl);
		String str;

		try {

			URL url = new URL(strUrl);
			JsonParserPromotion jsonParsProm = new JsonParserPromotion();

			//lire le text recu du serveur
			BufferedReader in = new BufferedReader(new InputStreamReader(url.openStream()));			
			while ((str = in.readLine()) != null) {
				Promotion p = jsonParsProm.jsonRead(str);
				promotions.add(p);
			}			
			in.close();
		} catch (MalformedURLException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}		
		return promotions;
	}
	
	public Promotion getPromotion(int idProm)
	{
		String strUrl  =("http://"+HOST.HOST+"/proxirabaisServer/PromotionServlet?"+SystemMessage.ACTION+"="+SystemMessage.GET_USER_PROMOTION+"&"+SystemMessage.PROMOTION_ID+"="+idProm);
		String str;
		Promotion p = new Promotion();
		
		try {
			URL url = new URL(strUrl);
			JsonParserPromotion jsonParsProm = new JsonParserPromotion();

			BufferedReader in = new BufferedReader(new InputStreamReader(url.openStream()));			
			if ((str = in.readLine()) != null) {
				 p = jsonParsProm.jsonRead(str);				
			}			
			in.close();
		} catch (MalformedURLException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}		
		return p;
	}

}
